package com.chixing.service.impl;

import com.chixing.mapper.MemberMapper;
import com.chixing.pojo.Member;
import com.chixing.pojo.MemberExample;
import com.chixing.service.CustomerService;
import com.chixing.util.RSAUtil;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.PrivateKey;
import java.util.List;
import java.util.UUID;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public ServerResult getById(Integer cusId) {

        Member member = memberMapper.selectByPrimaryKey(cusId);
        if (member != null)
            return ServerResult.getSussess(member);

        return ServerResult.getFail(null);
    }

    @Override
    public ServerResult login(Long cusTelno, String cusPwd){

        PrivateKey privateKey = null;
        try {
            privateKey = RSAUtil.string2PrivateKey(RSAUtil.PRIVATE_KEY_BASE64);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("私钥：" + privateKey);

        //解密
        byte[] publicEncrypt2 = new byte[0];
        try {
            publicEncrypt2 = RSAUtil.base642Byte(cusPwd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //用私钥解密，得到aesKey
        byte[] aesKeyStrBytes = new byte[0];
        try {
            aesKeyStrBytes = RSAUtil.privateDecrypt(publicEncrypt2,privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String aesKeyStr2 = new String(aesKeyStrBytes);

//        //aes 解密
//        String newPwd = AESUtil.desEncrypt(aesKeyStr2,AESUtil.AES_KEY,AESUtil.AES_IV);
//



        MemberExample example = new MemberExample();
        example.createCriteria().andMemberTelnoEqualTo(cusTelno).andMemberPwdEqualTo(aesKeyStr2);
        List<Member> memberList = memberMapper.selectByExample(example);

        if (memberList != null && memberList.size() > 0) {
            return ServerResult.getSussess(memberList.get(0));
        }
        return ServerResult.getFail(null);
    }

    @Override
    public ServerResult regist(Member member){

        System.out.println("service中的regist");
        PrivateKey privateKey = null;
        try {
            privateKey = RSAUtil.string2PrivateKey(RSAUtil.PRIVATE_KEY_BASE64);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("私钥：" + privateKey);

        //解密
        byte[] publicEncrypt2 = new byte[0];
        try {
            publicEncrypt2 = RSAUtil.base642Byte(member.getMemberPwd());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //用私钥解密，得到aesKey
        byte[] aesKeyStrBytes = new byte[0];
        try {
            aesKeyStrBytes = RSAUtil.privateDecrypt(publicEncrypt2,privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String aesKeyStr2 = new String(aesKeyStrBytes);

        MemberExample example = new MemberExample();
        example.createCriteria().andMemberTelnoEqualTo(member.getMemberTelno());

        List<Member> memberList = memberMapper.selectByExample(example);

        System.out.println("memberList：" + memberList);

        if (memberList != null && memberList.size() > 0) {
            return ServerResult.saveFail("该电话号码已注册");
        }

        member.setMemberPwd(aesKeyStr2);
        int rows = memberMapper.insert(member);

        System.out.println("member:" + member);

        if (rows > 0) {
            System.out.println("数据插入成功");
            return ServerResult.saveSussess(rows);
        }

        return ServerResult.saveFail(null);
    }

    @Override
    public ServerResult deleteUser(Member member) {
        int rows = memberMapper.updateByPrimaryKeySelective(member);

        if (rows > 0)
            return ServerResult.deleteSussess(rows);

        return ServerResult.deleteFail(null);
    }

    @Override
    public ServerResult updateCustomer(Member member) {
        int rows = memberMapper.updateByPrimaryKeySelective(member);

        if (rows > 0)
            return ServerResult.updateSussess(rows);
        return ServerResult.updateFail(null);
    }

    /**
     * 上传图片文件
     * @param file 图片
     * @return 返回本机tomcat保存图片的全路径和图片的名称的list
     */
    @Override
    public String[] saveFile(MultipartFile file) {
        if (file.isEmpty()) {
            System.out.println("文件不能为空");
            return null;
        }

        String fileName = file.getOriginalFilename();//获取上传文件的原名称
        System.out.println("图片原名称===" + fileName);

        // newName 图片到新名字
        String newName = newName(fileName);
        System.out.println("重命名之后的名称====" + newName);

        //本机图片全路径
        String filePath = System.getProperty("catalina.home") + "/uplode/" + newName;

        //用于保存 tomcat的upload路径 和图片名字
        String[] stringList = new String[] {filePath , newName};

        System.out.println(filePath);
        File dest = new File(filePath);

        try {
            if (!dest.exists()) {
                file.transferTo(dest);
            } else {
                System.out.println("已存在该文件");
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringList;
    }

    //设置新文件的文件名
    private String newName(String oldName) {//me.jpg
        String[] datas = oldName.split("\\.");
        String type = datas[datas.length - 1];
        String newName = UUID.randomUUID().toString() + "." + type;
        return newName;
    }


}
